using System.Collections.Generic;
using System.Text;
using Sedge.Core.Configuration;

namespace Sedge.Core.Execution.ReportElements
{
	public class ReportTable : ReportElementBase
	{
		private IEnumerable<string> _headers;
		private readonly List<IEnumerable<string>> _rows = new List<IEnumerable<string>>();

		public ReportTable(Suite suite)
			: base(suite)
		{
		}

		public override void MergeWith(IReportElement element)
		{
			ReportTable reportTable = element as ReportTable;
			if (reportTable != null)
			{
				foreach (IEnumerable<string> row in reportTable._rows)
				{
					AddRow(row);
				}
			}
		}

		public void SetHeader(IEnumerable<string> headerRow)
		{
			_headers = headerRow;
		}

		public void AddRow(IEnumerable<string> row)
		{
			_rows.Add(row);
		}

		public void AddRow(string col1, string col2)
		{
			_rows.Add( new[] {col1, col2} );
		}

		public override string GetHtml()
		{
			StringBuilder sb = new StringBuilder(GetHeaderAsHtml());
			sb.Append("<table class=\"report-table\">");
			
			if (_headers != null)
			{
				sb.Append("<tr>");
				foreach (string header in _headers)
				{
					sb.AppendFormat("<th>{0}</th>", header);
				}
				sb.Append("</tr>");
			}

			foreach (IEnumerable<string> row in _rows)
			{
				sb.Append("<tr>");
				foreach (string cell in row)
				{
					sb.AppendFormat("<td>{0}</td>", cell);
				}
				sb.Append("</tr>");
			}

			sb.Append("</table>");

			return sb.ToString();
		}
	}
}